package com.luo.ams.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luo.ams.service.IRoleService;
import com.luo.common.restful.JsonResult;
import com.luo.pojo.ams.dto.RoleAddDTO;
import com.luo.pojo.ams.entity.Role;
import com.luo.pojo.ams.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author bin.cn
 * @since 2023-01-19
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult getRoleList(){
        List<Role> list = roleService.list(new QueryWrapper<Role>().orderByAsc("sort"));
        return JsonResult.ok(list);
    }

    @GetMapping("/detail/{id}")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult getRoleDetail(@PathVariable Integer id){
        RoleVo detailById = roleService.getDetailById(id);
        return JsonResult.ok(detailById);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult addNew(RoleAddDTO roleAddDTO){
        roleService.addNew(roleAddDTO);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult update(RoleAddDTO roleAddDTO){
        roleService.updateByRoleId(roleAddDTO);
        return JsonResult.ok();
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult deleteById(@PathVariable Long id){
        roleService.deleteById(id);
        return JsonResult.ok();
    }



}
